package com.bidevalution.service.impl;

import com.bidevalution.core.constant.CharConstant;
import com.bidevalution.core.enums.ApiEnum;
import com.bidevalution.core.enums.PushActionEnum;
import com.bidevalution.core.util.WebServiceUtils;
import com.bidevalution.dao.ApiBidResultBackMapper;
import com.bidevalution.dao.ApiBidResultMapper;
import com.bidevalution.model.PushData;
import com.bidevalution.service.BidResultService;
import com.qtp.core.model.Response;
import com.qtp.core.util.DateUtil;
import com.qtp.core.util.GsonUtil;
import com.qtp.core.util.OConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author huyiqi
 * @date 2019/11/20
 */
@Service(value = "bidResultService")
@Transactional(rollbackFor = Exception.class)
public class BidResultServiceImpl extends BaseServiceImpl implements BidResultService {

    private static final Logger log = LoggerFactory.getLogger(BidResultServiceImpl.class);

    private static final String AFFICHE_TYPE = "18";
    private static final String FILE_TYPE = "pdf";
    private static final String FILE_DESC = "关于中标公示候选人的公示.pdf";
    private static final String IS_ZBWJ = "0";

    private Pattern winBidInfoPatten = Pattern.compile("\\$Final_WinBidInfo\\$(\\[.*?\\])");
    private Pattern segmentEvaluationRecordPatten = Pattern.compile("\\$SegmentEvaluationRecord\\$(\\[.*?\\])");
    private Pattern evaluationRecordPatten = Pattern.compile("\\$EvaluationRecord\\$(\\[.*?\\])");
    private Pattern invalidBidRecordPatten = Pattern.compile("\\$InvalidBidRecord\\$(\\[.*?\\])");
    private Pattern expertScoreRecordPatten = Pattern.compile("\\$ExpertScoreRecord\\$(\\[.*?\\])");
    private Pattern winBidScorePatten = Pattern.compile("\\$WinBidScore\\$(\\[.*?\\])");

    @Override
    public Response<String> pushBidResult(Map<String, Object> segmentEvaluationRecord,
                                          List<Map<String, Object>> evaluationRecord,
                                          List<Map<String, Object>> winBidPerformance,
                                          List<Map<String, Object>> invalidBidRecord,
                                          List<Map<String, Object>> expertScoreRecord,
                                          List<Map<String, Object>> winBidScore, List<Map<String, Object>> winBidInfo) {
        String nowTime = DateUtil.dateToString(new Date(), 2);
        String segmentShowID = OConvertUtils.getString(segmentEvaluationRecord.get("SegmentShowID"));
        String newGuid = UUID.randomUUID().toString();

        String oldGuid = pushProjectInfo(segmentEvaluationRecord, newGuid, segmentShowID, nowTime);
        setEvaluationRecordNew(newGuid, segmentShowID, nowTime);
        updateAndCreateEvaluationRecord(evaluationRecord, newGuid, segmentShowID, nowTime);
        deleteAndCreateWinBidPerformance(winBidPerformance, oldGuid, newGuid, segmentShowID, nowTime);
        deleteAndCreateInvalidBidRecord(invalidBidRecord, oldGuid, newGuid, segmentShowID, nowTime);
        deleteAndCreateExpertScoreRecord(expertScoreRecord, oldGuid, newGuid, segmentShowID, nowTime);
        deleteAndCreateWinBidScore(winBidScore, oldGuid, newGuid, segmentShowID, nowTime);
        updateAndCreateWinBidInfo(winBidInfo, segmentShowID, nowTime);

        return getBidResultBack(segmentShowID);
    }

    @Override
    public Response<String> pushPdf(String sectionCode, byte[] buff) {
        ApiEnum api = ApiEnum.GetFile;
        Date date = new Date();
        String now = DateUtil.dateToString(date, 2);
        String fileSize = OConvertUtils.getString(buff.length);

        Map<String, Object> segmentEvaluationRecord = bidResultMapper.getNewSegmentEvaluationRecord(sectionCode);
        if(segmentEvaluationRecord!=null) {
            String guid = OConvertUtils.getString(segmentEvaluationRecord.get("GUID"));
            try {
                log.info("准备开始推送中标公示pdf");
                log.info("参数：" + sectionCode + "，" + AFFICHE_TYPE + "，" + FILE_TYPE + "，" + FILE_DESC + "，" + fileSize + "，" + now);
                String result = WebServiceUtils.callWebSV(api.getUrl(), api.getMethod(), api.getAccount(), api.getPassword()
                        , sectionCode, AFFICHE_TYPE, FILE_TYPE, FILE_DESC, fileSize, now, now, now, IS_ZBWJ, buff, guid);
                log.info("推送中标公示pdf成功");
                if (result.equalsIgnoreCase(CharConstant.CHAR_TRUE)) {
                    return Response.ok();
                } else {
                    log.info("推送中标公示pdf失败：" + result);
                    return Response.fail(result);
                }
            } catch (Exception e) {
                log.error("推送中标公示PDF时出错", e);
                return Response.fail("推送中标公示PDF时出错：" + e.getMessage());
            }
        } else {
            return Response.fail("推送中标公示pdf，无法查找推送主表信息");
        }
    }

    private Response<String> getBidResultBack(String sectionCode) {
        ApiEnum api = ApiEnum.GetPBSGS;
        try {
            log.info("开始抓取数据");
            log.info("请求地址：{}",api.getUrl());
            log.info("请求方法：{}",api.getMethod());
            log.info("请求参数：{}{}{}", api.getAccount(), api.getPassword(), sectionCode);
            String result = WebServiceUtils.callWebSV(api.getUrl(), api.getMethod(), api.getAccount(), api.getPassword()
                    , sectionCode);
            log.info("返回结果：{}", result);
            if(StringUtils.isNotBlank(result)) {
                saveWinBidInfo(sectionCode, result);
                saveSegmentEvaluationRecord(sectionCode, result);
                saveEvaluationRecord(sectionCode, result);
                saveInvalidBidRecord(sectionCode, result);
                saveExpertScoreRecord(sectionCode, result);
                saveWinBidScore(sectionCode, result);
            }
        } catch (Exception e) {
            log.error("抓取数据失败：", e);
        }
        return Response.ok();
    }

    private String pushProjectInfo(Map<String, Object> segmentEvaluationRecord, String newGuid, String segmentShowID, String nowTime) {
        String waterMark = getWaterMark();
        String oldGuid = null;
        //查询并修改老的数据，将isnew改为0
        Map<String, Object> oldItem = bidResultMapper.getNewSegmentEvaluationRecord(segmentShowID);
        if(oldItem != null && !oldItem.isEmpty()) {
            oldGuid = OConvertUtils.getString(oldItem.get("GUID"));
            PushData updateOldData = new PushData();
            updateOldData.setHeader("SegmentEvaluationRecord", "GUID", nowTime, PushActionEnum.UPDATE)
                    .addRowField("GUID", oldGuid).addRowField("IsNew", "0");
            pushData(updateOldData, "更新五库原有项目信息");
            bidResultMapper.setSegmentEvaluationRecordOld(segmentShowID);
        }
        //向五库插入数据
        String buildUnit = OConvertUtils.getString(segmentEvaluationRecord.get("BuildUnit"));
        String segmentName = OConvertUtils.getString(segmentEvaluationRecord.get("SegmentName"));
        String evaluationPerson = OConvertUtils.getString(segmentEvaluationRecord.get("EvaluationPerson"));
        String evaluationStart = OConvertUtils.getString(segmentEvaluationRecord.get("EvaluationStart"));
        String expertAptitude = OConvertUtils.getString(segmentEvaluationRecord.get("ExpertAptitude"));
        String evaluationIdea = OConvertUtils.getString(segmentEvaluationRecord.get("EvaluationIdea"));
        String performanceRequire = OConvertUtils.getString(segmentEvaluationRecord.get("PerformanceRequire"));
        String reviewStandards = OConvertUtils.getString(segmentEvaluationRecord.get("ReviewStandards"));
        String dissidence = OConvertUtils.getString(segmentEvaluationRecord.get("Dissidence"));

        PushData insertData = new PushData();
        insertData.setHeader("SegmentEvaluationRecord", "GUID", nowTime, PushActionEnum.INSERT)
                .addRowField("GUID", newGuid).addRowField("SegmentShowID", segmentShowID)
                .addRowField("BuildUnit", buildUnit).addRowField("SegmentName",segmentName)
                .addRowField("EvaluationPerson", evaluationPerson).addRowField("EvaluationStart", evaluationStart)
                .addRowField("EvaluationEnd", nowTime).addRowField("CreateTime",nowTime).addRowField("Modifytime",nowTime)
                .addRowField("EvaluationNum", "1").addRowField("IsNew", "1").addRowField("ExpertAptitude", expertAptitude)
                .addRowField("Watermark", waterMark).addRowField("PublicityType", "1")
                .addRowField("EvaluationIdea", evaluationIdea).addRowField("PerformanceRequire", performanceRequire)
                .addRowField("ReviewStandards", reviewStandards).addRowField("Dissidence", dissidence);

        pushData(insertData, "新增五库项目信息");
        //本地标插入数据
        segmentEvaluationRecord.put("GUID", newGuid);
        segmentEvaluationRecord.put("SegmentShowID", segmentShowID);
        segmentEvaluationRecord.put("EvaluationEnd", nowTime);
        segmentEvaluationRecord.put("CreateTime", nowTime);
        segmentEvaluationRecord.put("Modifytime", nowTime);
        segmentEvaluationRecord.put("EvaluationNum", "1");
        segmentEvaluationRecord.put("IsNew", "1");
        segmentEvaluationRecord.put("Watermark", waterMark);
        segmentEvaluationRecord.put("PublicityType", "1");
        bidResultMapper.createSegmentEvaluationRecord(segmentEvaluationRecord);
        return oldGuid;
    }

    private void setEvaluationRecordNew(String newGuid, String segmentShowID, String nowTime) {
        Map<String, String> param = new HashMap<>(4);
        param.put("newGuid", newGuid);
        param.put("segmentShowID", segmentShowID);
        bidResultMapper.setEvaluationRecordNew(param);
        List<Map<String, Object>> ids = bidResultMapper.getEvaluationRecordIds(segmentShowID);
        if(ids != null) {
            for(Map<String, Object> map:ids) {
                if(map!=null) {
                    String id = OConvertUtils.getString(map.get("GUID"));
                    PushData data = new PushData();
                    data.setHeader("EvaluationRecord", "GUID", nowTime, PushActionEnum.DELETE)
                        .addRowField("GUID", id);
                    pushData(data, "更新专家验证信息，删除五库EvaluationRecord表");
                }
            }
        }
    }

    private void updateAndCreateEvaluationRecord(List<Map<String, Object>> evaluationRecord, String newGuid, String segmentShowID, String nowTime) {
        if(evaluationRecord != null) {
            for(Map<String, Object> map:evaluationRecord) {
                if(map==null) {
                    continue;
                }
                map.put("EvaluationEnd", nowTime);
                map.put("SegmentShowID", segmentShowID);
                map.put("newGuid", newGuid);
                String yzdb = OConvertUtils.getString(map.get("YZDB"));
                if(!"1".equals(yzdb)) {
                    map.remove("YZDB");
                }
                bidResultMapper.updateEvaluationRecord(map);
                Map<String, Object> record = bidResultMapper.selectEvaluationRecord(map);
                if(record != null) {
                    PushData data = new PushData();
                    data.setHeader("EvaluationRecord", "GUID", nowTime, PushActionEnum.INSERT)
                            .addRowField("GUID", OConvertUtils.getString(record.get("GUID")))
                            .addRowField("SegmentShowID", segmentShowID)
                            .addRowField("SegmentEvaluationRecordGuid", newGuid)
                            .addRowField("PersonGuid", OConvertUtils.getString(record.get("PersonGuid")))
                            .addRowField("SingleTime", nowTime)
                            .addRowField("EvaluationEnd", nowTime)
                            .addRowField("PersonIdea", OConvertUtils.getString(record.get("PersonIdea")))
                            .addRowField("CreateTime", nowTime)
                            .addRowField("Modifytime", nowTime)
                            .addRowField("WorkUnit", OConvertUtils.getString(record.get("WorkUnit")))
                            .addRowField("Valid", OConvertUtils.getString(record.get("Valid")))
                            .addRowField("SpecialistProfessional", OConvertUtils.getString(record.get("SpecialistProfessional")))
                            .addRowField("SpecialistType", OConvertUtils.getString(record.get("SpecialistType")))
                            .addRowField("ExpertAptitude", OConvertUtils.getString(record.get("ExpertAptitude")))
                            .addRowField("IDcard", OConvertUtils.getString(record.get("IDcard")))
                            .addRowField("Name", OConvertUtils.getString(record.get("Name")))
                            .addRowField("PersonCode", OConvertUtils.getString(record.get("PersonCode")));
                    pushData(data, "更新专家验证信息，插入五库EvaluationRecord表");
                }
            }
        }
    }

    private void deleteAndCreateWinBidPerformance(List<Map<String, Object>> winBidPerformance, String oldGuid, String newGuid, String segmentShowID, String nowTime) {
        if(winBidPerformance != null) {
            Integer idx = 1;
            String conUnitName = "";
            if(StringUtils.isNotBlank(oldGuid)) {
                Map<String, String> param = new HashMap<>(4);
                param.put("oldGuid", oldGuid);
                param.put("segmentShowID", segmentShowID);
                List<Map<String, Object>> oldWinBidPerformance = bidResultMapper.getOldWinBidPerformance(param);
                if(oldWinBidPerformance != null) {
                    for(Map<String, Object> map:oldWinBidPerformance) {
                        PushData data = new PushData();
                        data.setHeader("WinBidPerformance", "GUID", nowTime, PushActionEnum.DELETE)
                                .addRowField("GUID", OConvertUtils.getString(map.get("GUID")));
                        pushData(data, "删除五库中标候选人业绩");
                    }
                }
                bidResultMapper.deleteOldWinBidPerformance(param);
            }
            for(Map<String, Object> map:winBidPerformance) {
                if(map==null) {
                    continue;
                }
                String guid = UUID.randomUUID().toString();
                map.put("GUID", guid);
                map.put("ConUnitName", conUnitName);
                map.put("SortIndex", idx.toString());
                map.put("newGuid", newGuid);
                map.put("SegmentShowID", segmentShowID);
                bidResultMapper.createWinBidPerformance(map);

                PushData data = new PushData();
                data.setHeader("WinBidPerformance", "GUID", nowTime, PushActionEnum.INSERT)
                        .addRowField("GUID", guid)
                        .addRowField("EvaluationRecordGuid", newGuid)
                        .addRowField("SegmentShowID", segmentShowID)
                        .addRowField("PerformanceCertify", OConvertUtils.getString(map.get("PerformanceCertify")))
                        .addRowField("PerformanceName", OConvertUtils.getString(map.get("PerformanceName")))
                        .addRowField("UnitName", OConvertUtils.getString(map.get("UnitName")))
                        .addRowField("Requirements", OConvertUtils.getString(map.get("Requirements")))
                        .addRowField("Beweismaterial", OConvertUtils.getString(map.get("Beweismaterial")))
                        .addRowField("Conclusion", OConvertUtils.getString(map.get("Conclusion")))
                        .addRowField("ZZJGDM", OConvertUtils.getString(map.get("ZZJGDM")))
                        .addRowField("ConUnitName", conUnitName)
                        .addRowField("Location", OConvertUtils.getString(map.get("Location")))
                        .addRowField("SortIndex", idx.toString());
                pushData(data, "新增五库中标候选人业绩");
                idx++;
            }
        }
    }

    private void deleteAndCreateInvalidBidRecord(List<Map<String, Object>> invalidBidRecord, String oldGuid, String newGuid, String segmentShowID, String nowTime) {
        if(invalidBidRecord != null) {
            if(StringUtils.isNotBlank(oldGuid)) {
                Map<String, String> param = new HashMap<>(4);
                param.put("oldGuid", oldGuid);
                param.put("segmentShowID", segmentShowID);
                List<Map<String, Object>> oldInvalidBidRecord = bidResultMapper.getOldInvalidBidRecord(param);
                if(oldInvalidBidRecord != null) {
                    for(Map<String, Object> map:oldInvalidBidRecord) {
                        PushData data = new PushData();
                        data.setHeader("InvalidBidRecord", "GUID", nowTime, PushActionEnum.DELETE)
                                .addRowField("GUID", OConvertUtils.getString(map.get("GUID")));
                        pushData(data, "删除五库废标情况");
                    }
                }
                bidResultMapper.deleteOldInvalidBidRecord(param);
            }
            for(Map<String, Object> map:invalidBidRecord) {
                if(map==null) {
                    continue;
                }
                String guid = UUID.randomUUID().toString();
                map.put("GUID", guid);
                map.put("newGuid", newGuid);
                map.put("SegmentShowID", segmentShowID);
                bidResultMapper.createInvalidBidRecord(map);

                PushData data = new PushData();
                data.setHeader("InvalidBidRecord", "GUID", nowTime, PushActionEnum.INSERT)
                        .addRowField("GUID", guid)
                        .addRowField("EvaluationRecordGuid", newGuid)
                        .addRowField("SegmentShowID", segmentShowID)

                        .addRowField("ZZJGDM", OConvertUtils.getString(map.get("ZZJGDM")))
                        .addRowField("UnitName", OConvertUtils.getString(map.get("UnitName")))
                        .addRowField("Problems", OConvertUtils.getString(map.get("Problems")))
                        .addRowField("Clause", OConvertUtils.getString(map.get("Clause")));
                pushData(data, "新增五库废标情况");
            }
        }
    }

    private void deleteAndCreateExpertScoreRecord(List<Map<String, Object>> expertScoreRecord, String oldGuid, String newGuid, String segmentShowID, String nowTime) {
        if(expertScoreRecord != null) {
            if(StringUtils.isNotBlank(oldGuid)) {
                Map<String, String> param = new HashMap<>(4);
                param.put("oldGuid", oldGuid);
                param.put("segmentShowID", segmentShowID);
                List<Map<String, Object>> oldExpertScoreRecord = bidResultMapper.getOldExpertScoreRecord(param);
                if(oldExpertScoreRecord != null) {
                    for(Map<String, Object> map:oldExpertScoreRecord) {
                        PushData data = new PushData();
                        data.setHeader("ExpertScoreRecord", "GUID", nowTime, PushActionEnum.DELETE)
                                .addRowField("GUID", OConvertUtils.getString(map.get("GUID")));
                        pushData(data, "删除五库专家评分情况");
                    }
                }
                bidResultMapper.deleteOldExpertScoreRecord(param);
            }
            for(Map<String, Object> map:expertScoreRecord) {
                if(map==null) {
                    continue;
                }
                String guid = UUID.randomUUID().toString();
                map.put("GUID", guid);
                map.put("newGuid", newGuid);
                map.put("SegmentShowID", segmentShowID);
                bidResultMapper.createExpertScoreRecord(map);

                PushData data = new PushData();
                data.setHeader("ExpertScoreRecord", "GUID", nowTime, PushActionEnum.INSERT)
                        .addRowField("GUID", guid)
                        .addRowField("EvaluationRecordGuid", newGuid)
                        .addRowField("SegmentShowID", segmentShowID)

                        .addRowField("PersonGuid", OConvertUtils.getString(map.get("PersonGuid")))
                        .addRowField("Name", OConvertUtils.getString(map.get("Name")))
                        .addRowField("IDcard", OConvertUtils.getString(map.get("IDcard")))
                        .addRowField("PersonCode", OConvertUtils.getString(map.get("PersonCode")))
                        .addRowField("ZZJGDM", OConvertUtils.getString(map.get("ZZJGDM")))
                        .addRowField("UnitName", OConvertUtils.getString(map.get("UnitName")))
                        .addRowField("UnitScore", OConvertUtils.getString(map.get("UnitScore")));
                pushData(data, "新增五库专家评分情况");
            }
        }
    }

    private void deleteAndCreateWinBidScore(List<Map<String, Object>> winBidScore, String oldGuid, String newGuid, String segmentShowID, String nowTime) {
        if(winBidScore != null) {
            if(StringUtils.isNotBlank(oldGuid)) {
                Map<String, String> param = new HashMap<>(4);
                param.put("oldGuid", oldGuid);
                param.put("segmentShowID", segmentShowID);
                List<Map<String, Object>> oldWinBidScore = bidResultMapper.getOldWinBidScore(param);
                if(oldWinBidScore != null) {
                    for(Map<String, Object> map:oldWinBidScore) {
                        PushData data = new PushData();
                        data.setHeader("WinBidScore", "GUID", nowTime, PushActionEnum.DELETE)
                                .addRowField("GUID", OConvertUtils.getString(map.get("GUID")));
                        pushData(data, "删除五库单位得分信息");
                    }
                }
                bidResultMapper.deleteOldWinBidScore(param);
            }
            for(Map<String, Object> map:winBidScore) {
                if(map==null) {
                    continue;
                }
                String guid = UUID.randomUUID().toString();
                map.put("GUID", guid);
                map.put("newGuid", newGuid);
                map.put("SegmentShowID", segmentShowID);
                bidResultMapper.createWinBidScore(map);

                PushData data = new PushData();
                data.setHeader("WinBidScore", "GUID", nowTime, PushActionEnum.INSERT)
                        .addRowField("GUID", guid)
                        .addRowField("EvaluationRecordGuid", newGuid)
                        .addRowField("SegmentShowID", segmentShowID)

                        .addRowField("UnitName", OConvertUtils.getString(map.get("mysUnitName")))
                        .addRowField("ZZJGDM", OConvertUtils.getString(map.get("ZZJGDM")))
                        .addRowField("UnitZXScore", OConvertUtils.getString(map.get("UnitZXScore")))
                        .addRowField("UnitSWScore", OConvertUtils.getString(map.get("UnitSWScore")));
                pushData(data, "新增五库单位得分信息");
            }
        }
    }

    private void updateAndCreateWinBidInfo(List<Map<String, Object>> winBidInfo, String segmentShowID, String nowTime) {
        if(winBidInfo != null) {
            List<Map<String, Object>> oldItems = bidResultMapper.getOldWinBidInfo(segmentShowID);
            //修改原有的为老
            if(oldItems != null) {
                for(Map<String, Object> old:oldItems) {
                    PushData data = new PushData();
                data.setHeader("WinBidInfo", "GUID", nowTime, PushActionEnum.UPDATE)
                        .addRowField("GUID", OConvertUtils.getString(old.get("GUID")))
                        .addRowField("IsNew", "0");
                pushData(data, "更新五库中标单位为老");
            }
                bidResultMapper.setWinBidInfoOld(segmentShowID);
            }
            //插入新数据
            for(Map<String, Object> map:winBidInfo) {
                if(map==null) {
                    continue;
                }
                String guid = UUID.randomUUID().toString();
                map.put("GUID", guid);
                map.put("IsNew", "1");
                map.put("PersonnelGUID", "");
                map.put("EnterpriseGUID", "");
                bidResultMapper.createWinBidInfo(map);

                PushData data = new PushData();
                data.setHeader("WinBidInfo", "GUID", nowTime, PushActionEnum.INSERT)
                        .addRowField("GUID", guid)
                        .addRowField("XMBH", OConvertUtils.getString(map.get("XMBH")))
                        .addRowField("XMMC", OConvertUtils.getString(map.get("XMMC")))
                        .addRowField("JSDW", OConvertUtils.getString(map.get("JSDW")))
                        .addRowField("ZBPX", OConvertUtils.getString(map.get("ZBPX")))
                        .addRowField("DWMC", OConvertUtils.getString(map.get("DWMC")))
                        .addRowField("ZZJGDM", OConvertUtils.getString(map.get("ZZJGDM")))
                        .addRowField("XMJL", OConvertUtils.getString(map.get("XMJL")))
                        .addRowField("SFZH", OConvertUtils.getString(map.get("SFZH")))
                        .addRowField("GQ", OConvertUtils.getString(map.get("GQ")))
                        .addRowField("TBBJ", OConvertUtils.getString(map.get("TBBJ")))
                        .addRowField("PBSJ", OConvertUtils.getString(map.get("PBSJ")))
                        .addRowField("TSSJ", OConvertUtils.getString(map.get("TSSJ")))
                        .addRowField("tbkzj", OConvertUtils.getString(map.get("tbkzj")))
                        .addRowField("Fjzbj", OConvertUtils.getString(map.get("Fjzbj")))
                        .addRowField("pbbf", OConvertUtils.getString(map.get("pbbf")))
                        .addRowField("DWDF", OConvertUtils.getString(map.get("DWDF")))
                        .addRowField("biin_zczjgcs", OConvertUtils.getString(map.get("biin_zczjgcs")))
                        .addRowField("biin_zjsdw", OConvertUtils.getString(map.get("biin_zjsdw")))
                        .addRowField("IsNew", OConvertUtils.getString(map.get("1")))
                        .addRowField("PersonnelGUID", OConvertUtils.getString(map.get("")))
                        .addRowField("EnterpriseGUID", OConvertUtils.getString(map.get("")));
                pushData(data, "插入五库中标单位信息");
            }
        }
    }

    private void pushData(PushData data, String type) {
        ApiEnum api = ApiEnum.Accept_Service_Data;
        try {
            invokeFxWebService(api, api.getAccount(), api.getPassword(), data.toString());
        } catch (Exception e) {
            log.error(type+"时出错", e);
        }
    }

    private void saveWinBidInfo(String sectionCode, String result) {
        List<Map<String, Object>> list = getListFromResult(result, winBidInfoPatten);
        if(list != null) {
            bidResultBackMapper.deleteWinBidInfo(sectionCode);
            for(Map<String, Object> map:list) {
                bidResultBackMapper.insertWinbidinfo(map);
            }
        }
    }
    private void saveSegmentEvaluationRecord(String sectionCode, String result) {
        List<Map<String, Object>> list = getListFromResult(result, segmentEvaluationRecordPatten);
        if(list != null) {
            bidResultBackMapper.deleteSegmentEvaluationRecord(sectionCode);
            for(Map<String, Object> map:list) {
                bidResultBackMapper.insertSegmentEvaluationRecord(map);
            }
        }
    }
    private void saveEvaluationRecord(String sectionCode, String result) {
        List<Map<String, Object>> list = getListFromResult(result, evaluationRecordPatten);
        if(list != null) {
            bidResultBackMapper.deleteEvaluationRecord(sectionCode);
            for(Map<String, Object> map:list) {
                bidResultBackMapper.insertEvaluationRecord(map);
            }
        }
    }
    private void saveInvalidBidRecord(String sectionCode, String result) {
        List<Map<String, Object>> list = getListFromResult(result, invalidBidRecordPatten);
        if(list != null) {
            bidResultBackMapper.deleteInvalidBidRecord(sectionCode);
            for(Map<String, Object> map:list) {
                bidResultBackMapper.insertInvalidBidRecord(map);
            }
        }
    }
    private void saveExpertScoreRecord(String sectionCode, String result) {
        List<Map<String, Object>> list = getListFromResult(result, expertScoreRecordPatten);
        if(list != null) {
            bidResultBackMapper.deleteExpertScoreRecord(sectionCode);
            for(Map<String, Object> map:list) {
                bidResultBackMapper.insertExpertScoreRecord(map);
            }
        }
    }

    private void saveWinBidScore(String sectionCode, String result) {
        List<Map<String, Object>> list = getListFromResult(result, winBidScorePatten);
        if(list != null) {
            bidResultBackMapper.deleteWinBidScore(sectionCode);
            for(Map<String, Object> map:list) {
                bidResultBackMapper.insertWinBidScore(map);
            }
        }
    }

    private List<Map<String, Object>> getListFromResult(String result, Pattern pattern) {
        List<Map<String, Object>> list = new ArrayList<>();
        Matcher m = pattern.matcher(result);
        if(m.find()) {
            String json = m.group(1);
            if(StringUtils.isNotBlank(json)) {
                if(isObjectJson(json)) {
                    Map<String, Object> map = GsonUtil.jsonToMapWithGson(json);
                    list.add(map);
                } else if(isArrayJson(json)) {
                    list = GsonUtil.jsonToListMapWithGson(json);
                }
            }
        }
        return list;
    }


    private String getWaterMark() {
        return OConvertUtils.getString((Math.random() * 9 + 1) * 100000);
    }

    @Autowired(required = false)
    private ApiBidResultMapper bidResultMapper;
    @Autowired(required = false)
    private ApiBidResultBackMapper bidResultBackMapper;
}
